package com.ght.springboot.rabbitmq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.UUID;

/**
 * @description: 消息发送类
 * @author: haitao.gao
 * @create: 2019-01-09 15:03
 **/
@Component
public class MessageSender implements RabbitTemplate.ConfirmCallback {

    @Autowired
    @Qualifier("rabbitTemplateForBusiness")
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
    }

    public void sendMsg(String content,String routingKey){
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(RabbitmqConfiguration.ExchangeDefault,routingKey,content,correlationData);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.print("发送消息唯一标识符："+correlationData.toString());
        System.out.print("发送确认结果：" + ack);
        System.out.print("生产者发送失败原因：" + cause);
    }
}
